import axios from 'axios'
import qs from 'qs'
import 'es6-promise'
import { Notification, MessageBox, Message } from 'element-ui'
import { getToken } from '@/utils/auth.js'
import Vue from 'vue'
import context from '../main.js'
console.log(context)
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
axios.defaults.baseURL = '/';
// axios.defaults.baseURL ="/request";


let app_power_sign=""
export function setHeader(methods,key,value){
    app_power_sign=value;
}
export function setHeaderClear(methods){
    app_power_sign=""
}
export function replaceUrl(url,type){
	return url
}
export function delFun(url, params,slash) {

    return new Promise((resolve, reject) => {
        axios.delete(url,{
			params: params,
			headers: {
				'Authorization':'Bearer '+ getToken(),
				'signature':getToken(),
			}
            }).then(res => {
				if(res.data.error_code==0){
					if(res.data.data==null){
						res.data.data=[]
					}
					resolve(res.data)
				}
			}).catch(err => {
				if(err.response.status==401){
				}else{
					let mesasageList=err.response.data
				
					let msg="程序出错了"
					// context.loading=false
					try{
						msg=JSON.parse(mesasageList).msg
						Message({
							message: msg,
							type: 'error'
						})
					}
					catch{
						Message({
							message: "程序出错了",
							type: 'error'
						})
					}
					resolve({errcode:1})
				}
			})
		})
	}

export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
        params: params,
        headers: {
            'Authorization':'Bearer '+ getToken(),
				'signature':getToken(),
        }
		}).then(res => {
			
			if(res.data.error_code==0){
				if(res.data.data==null){
					res.data.data=[]
				}
				resolve(res.data)
			}
		}).catch(err => {
			if(err.response.status==401){
				MessageBox.confirm(
					'登录状态已过期，请重新登录',
					'系统提示',{
						confirmButtonText: '重新登录',
						cancelButtonText: '取消',
						type: 'warning'
					}
				).then(() => {
					window.location.href="#/login"
				})
			}else if(err.response.status==504){
				resolve({errcode:504})
			}else{
				let mesasageList=err.response.data
			
				let msg="程序出错了"
				// context.loading=false
				try{
					msg=JSON.parse(mesasageList).msg
					Message({
						message: msg,
						type: 'error'
					})
				}
				catch{
					Message({
						message: "程序出错了",
						type: 'error'
					})
				}
				resolve({errcode:1})
			}
		})
    })
}

export function put(url, data) {
    return new Promise((resolve, reject) => {
        axios.put(url, qs.stringify(data), {
            headers: {
               'Authorization':'Bearer '+ getToken(),
				'signature':getToken(),
            }
			}).then(res => {
				if(res.data.error_code==0){
					if(res.data.data==null){
						res.data.data=[]
					}
					resolve(res.data)
				}
			}).catch(err => {
				if(err.response.status==401){
					MessageBox.confirm(
						'登录状态已过期，请重新登录',
						'系统提示',
						{
						confirmButtonText: '重新登录',
						cancelButtonText: '取消',
						type: 'warning'
						}
					).then(() => {
						window.location.href="#/login"
					})
				}else{
					let mesasageList=err.response.data
				
					let msg="程序出错了"
					// context.loading=false
					try{
						msg=JSON.parse(mesasageList).msg
						Message({
							message: msg,
							type: 'error'
						})
					}
					catch{
						Message({
							message: "程序出错了",
							type: 'error'
						})
					}
					resolve({errcode:1})
				}
				
					
			})
    })
}

export function post(url, data,bodyData) {
	let ContentType="application/x-www-form-urlencoded"
	let postData=qs.stringify(data)
	if(url.indexOf('upload')>-1){
		postData=bodyData;
		ContentType="multipart/form-data"
	}
	console.log(postData)
    return new Promise((resolve, reject) => {
        axios.post(url,postData, {
            headers: {
               'Authorization':'Bearer '+ getToken(),
				'signature':getToken(),
				"Content-Type":ContentType
            }
        }).then(res => {
				
			if(res.data.error_code==0){
				if(res.data.data==null){
					res.data.data=[]
				}
				resolve(res.data)
			}
		}).catch(err => {
			console.log(err)
			if(err.response.status==401){
				MessageBox.confirm(
					'登录状态已过期，请重新登录',
					'系统提示',
					{
						confirmButtonText: '重新登录',
						cancelButtonText: '取消',
						type: 'warning'
					}
				).then(() => {
					window.location.href="#/login"
				})
			}else if(err.response.status==504){
				resolve({errcode:504})
			}else{
				let mesasageList=err.response.data
			
				let msg="程序出错了"
				// context.$root.wk_loading.close();
				try{
					msg=JSON.parse(mesasageList).msg
					Message({
						message: msg,
						type: 'error'
					})
				}
				catch{
					Message({
						message: "程序出错了",
						type: 'error'
					})
				}
				resolve({errcode:1})
			}
		})
    })
}
export function upload(url, data,bodyData){

	let ContentType="multipart/form-data"
	let postData=qs.stringify(data)

    return new Promise((resolve, reject) => {
        axios.post(url,postData, {
            headers: {
               'Authorization':'Bearer '+ getToken(),
				'signature':getToken(),
				"Content-Type":ContentType
            }
        }).then(res => {
				
			if(res.data.error_code==0){
				if(res.data.data==null){
					res.data.data=[]
				}
				resolve(res.data)
			}
		}).catch(err => {
			console.log(err)
			if(err.response.status==401){
				MessageBox.confirm(
					'登录状态已过期，请重新登录',
					'系统提示',
					{
						confirmButtonText: '重新登录',
						cancelButtonText: '取消',
						type: 'warning'
					}
				).then(() => {
					window.location.href="#/login"
				})
			}else if(err.response.status==504){
				resolve({errcode:504})
			}else{
				let mesasageList=err.response.data
			
				let msg="程序出错了"
				// context.$root.wk_loading.close();
				try{
					msg=JSON.parse(mesasageList).msg
					Message({
						message: msg,
						type: 'error'
					})
				}
				catch{
					Message({
						message: "程序出错了",
						type: 'error'
					})
				}
				resolve({errcode:1})
			}
		})
    })

}

